{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 从高位数据向低维数据的映射\n",
    "> 前面推导除了获取原始数据的n个主成分的方法，这一节就用上一节封装的方法完成PCA最大的作用，即数据降维"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 降维 \n",
    "> 把原始数据X从n维降维到k维(从n列降为k列，每列的含义是一个特征，每行是一个样本)\n",
    "![Xn降维到Xk减少了特征数或维度数](images/Xn降维到Xk减少了特征数或维度数.png)\n",
    "```python\n",
    "def transform(self, X):\n",
    "    \"\"\"将给定的X，映射到各个主成分分量中\"\"\"\n",
    "    assert X.shape[1] == self.components_.shape[1]\n",
    "    return X.dot(self.components_.T)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 还原\n",
    "> 利用Xk点乘Wk完成升维，近似恢复到原始数据X，但是不可能完全恢复地，因为降维过程中的数据丢失是必可避免而且不可逆地\n",
    "![数据还原或升维但是不可能完全还原地](images/数据还原或升维但是不可能完全还原地.png)\n",
    "```python\n",
    "def inverse_transform(self, X):\n",
    "    \"\"\"将给定的X，反向映射回原来的特征空间\"\"\"\n",
    "    assert X.shape[1] == self.components_.shape[0]\n",
    "    return X.dot(self.components_)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 上面的降维和还原地过程都封装在`playML/PCA.py`里面了，下面利用封装的方法进行测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "X = np.empty((100, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X[:, 0] = np.random.uniform(0., 100., size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "X[:, 1] = 0.75 * X[:, 0] + 3. + np.random.normal(0, 10., size=100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 2)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[69.76737964, 55.17569708],\n",
       "       [62.96939249, 35.42724014],\n",
       "       [63.52415618, 52.31341863],\n",
       "       [43.61006368, 44.79007066],\n",
       "       [ 6.9956719 , 10.89434416]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X[:5, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "from playML.PCA import PCA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "pca = PCA(n_components=2) # 对2维数据进行PCA转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PCA(n_components=2)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.fit(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.74910873,  0.66244706],\n",
       "       [ 0.66244957, -0.74910651]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.components_ # X是二维的(两列)，所以只有两个主成分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.349524059836906e-06"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.components_[0].dot(pca.components_[1]) # 相邻两个主成分向量是垂直的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 下面把二维数据降维到1维"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PCA(n_components=1)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca = PCA(n_components=1) # 降到1维\n",
    "pca.fit(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.74910883, 0.66244694]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pca.components_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_reduction = pca.transform(X) # 降维后的X为X_reduction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 1)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_reduction.shape # 从(100, 2)变成了(100, 1)，从二维降到了一维"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 把降维后的数据恢复回去\n",
    "> 不可能完全恢复，因为数据丢失是必然的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(100, 2)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_restore = pca.inverse_transform(X_reduction)\n",
    "X_restore.shape # 可以看到维度又恢复回去了"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 下面对比下降维和恢复地数据\n",
    "> 恢复后的数据全聚集在第1主成分上了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXkAAAD4CAYAAAAJmJb0AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjAsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+17YcXAAAgAElEQVR4nO3de3Tb1ZXo8e+WZNmyEz+S2HHixHmQJ2RU4LptoC2vPm7ptNDlSTtMeykt3DKFMnRmYFFayr3uSul0WujQDEO6mGlnGIYOpR63ycyallehvQVCcUKqJoQQCKnjPB3iOI7fks7940ixokiyZP1kPbw/a3nZeli/n6xka2ufc/YRYwxKKaVKkyvfJ6CUUip3NMgrpVQJ0yCvlFIlTIO8UkqVMA3ySilVwjz5PoFYc+bMMYsXL873aSilVFHZunXrMWNMfaLbCirIL168mM7OznyfhlJKFRUR+UOy27Rco5RSJUyDvFJKlTAN8kopVcI0yCulVAnTIK+UUiVMg7xSSpUwDfJKKVXCNMgrpVQJK6jFUEopNS0EAtDRAV1d0NwMra3g9+fkUJrJK6XUVAoE4N57obcXFiyw3++9116fAxrklVJqKnV0QF2d/XK5xn/u6MjJ4bRco5QqClNY4citri6bwUccOQK7Xq3BfbCLZ3D+eWkmr5QqeFNc4cit5mbo6wNsgH/hBaCvj7H5zTl5XhrklVIFb4orHLnV2mrfpXp72fVqmDrppdb08trq1pw8Lw3ySqmC19UFNTVnXldTY68vOn4/3H471NXhPthNuKaOFy66naONtkbj9PPSmrxSquA1N9vkt65u/Lq+Pnt9UfL7we/nGXL/vDSTV0oVvJgKB+Hw+M+trXk+sUAA2trg+uvt9wyL6VPxvDTIK6UKXkyFg+5u+/322/M8u6a9Ha69Fh5/HN58E/bsyXjUdCqelxhjsn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Yyy2Yt0Bct6yRljNGvNL+Aj2D3sn0Tu2lK3s/Jgef0XuzHvQCwPfL1EWzd+hlgT+T7rHyfqwPP9TLgvyI/LwV+i92a8idAeb7Pz4Hndz7QGXktfwbUldrrCHwdeA3YATyC7YJb1K8l8O/YMYYxbKZ+Q7LXDVuu+YdIDPo9dqZRysfXtgZKKVXCtFyjlFIlTIO8UkqVMA3ySilVwjTIK6VUCdMgr5RSJUyDvFJKlTAN8kopVcL+P8Ze1yDAQthUAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X[:, 0], X[:, 1], color='b', alpha=0.5) # 原始数据，用蓝色表示\n",
    "plt.scatter(X_restore[:, 0], X_restore[:, 1], color='r', alpha=0.5) # 降维后恢复地数据，可以看到全部都聚集到第1主成分上了\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
